package atomic;

import java.util.concurrent.atomic.AtomicInteger;


// AtomicInteger实现多线程自增同一个变量
public class Main {
    public static void main(String[] args) throws InterruptedException {
        AtomicInteger atomicInteger = new AtomicInteger();

        Thread t1 = new Thread(()->{
            for (int i = 0; i < 10000; i++) {
                atomicInteger.getAndIncrement();
            }
        });
        Thread t2 = new Thread(()->{
            for (int i = 0; i < 10000; i++) {
                atomicInteger.getAndIncrement();
            }
        });
        t1.start();
        t2.start();
        t1.join();
        t2.join();

        System.out.println(atomicInteger.get());
    }
}
